package com.acxy.cms.controller;

/**
 * @author MrShun
 * @version 1.0
 * @Date 2017-07-28
 */

import com.acxy.ants.core.annotation.Autowired;
import com.acxy.ants.resful.annotation.Controller;
import com.acxy.ants.resful.annotation.GET;
import com.acxy.ants.resful.annotation.POST;
import com.acxy.ants.resful.annotation.Param;
import com.acxy.ants.resful.render.Json;
import com.acxy.ants.resful.render.Resource;
import com.acxy.cms.entity.Category;
import com.acxy.cms.service.CategoryService;

import java.util.List;

/**
 * @author MrShun
 * @version 1.0
 * @Date 2017-06-23
 */
@Controller("category")
public class CategoryAjax {

    @Autowired
    private CategoryService categoryService;


    @POST("/list")
    public List<Category> list(String module) {
        List<Category> result = categoryService.queryList(module);
        return result;
    }

    @POST("/all")
    public List<Category> all(String module) {
        List<Category> result = categoryService.queryAll(module);
        return result;
    }

    @POST("/delete")
    public Object delete(Long[] ids) {
        int result = categoryService.delete(ids);
        return Json.success(result);
    }

    /**
     * 保存栏目数据
     *
     * @param category 栏目数据
     * @return
     */
    @POST("/save")
    public Object save(Category category) {
        int res = categoryService.save(category);
        if (res == -1) return Json.fail("添加失败, 目录已存在!");
        return Json.success("ok");
    }

    /**
     * 修改栏目
     *
     * @param category 栏目数据
     * @return
     */
    @POST("/update")
    public Object update(Category category) {
        int res = categoryService.update(category);
        if (res == -1) return Json.fail("修改失败, 目录已存在!");
        return Json.success("ok");
    }

    @GET("/find")
    public Object find(Long id) {
        Category result = categoryService.find(id);
        return Json.success(result);
    }

    @POST("/updateSort")
    public Object updateSort(@Param Long id, Integer sort) {
        int res = categoryService.updateSort(id, sort);
        return Json.success(res);
    }

    @POST("/updateNav")
    public Object updateNav(@Param Long id, Integer nav) {
        int res = categoryService.updateNav(id, nav);
        return Json.success(res);
    }
}
